package com.irdstudio.apicenter.core.code.generator.veloctity.html;

import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import com.irdstudio.apicenter.core.code.generator.veloctity.VelocityCodeGenerator;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamMain;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamTag;

/**
 * HTML代码生成模板数据构造
 * 
 * @author ZhangHao
 * @date 2015-07-10
 * 
 */
public class HTMLCodeGenerator extends VelocityCodeGenerator {

	private static XMLConfiguration configuration;

	@Override
	public void constructionTemplet(List<?> tempData) throws Exception {

		try {
			if (tempData == null || tempData.isEmpty()) {
				logger.error("tempData is empty.");
				return;
			}

			SPageParamMain paramMain = null;
			String ttype = null;
			String code = null;
			String value = null;
			List<SPageParamTag> paramTags = null;
			for (Object sdata : tempData) {
				paramMain = (SPageParamMain) sdata;
				ttype = paramMain.getTemplateParamType();
				code = paramMain.getTemplateParamCode();
				value = paramMain.getParamValue();

				switch (Integer.valueOf(ttype)) {
				case 1: // 键值类型
					logger.debug("put data :{ code=" + code + ",value=" + value
							+ "}");
					context.put(code, value);

					break;
				case 2: // 标签类型

					paramTags = paramMain.getsPageParamTags();
					Validate.notEmpty(paramTags, "code :" + code
							+ " paramTags is empty.");

					StringBuffer tagbuff = new StringBuffer();
					String tagType = null;
					String tagValueSrc = null;
					for (SPageParamTag paramTag : paramTags) {
						tagType = paramTag.getTagType();
						tagValueSrc = paramTag.getTagValueSrc();

						tagGenerator(tagType, tagValueSrc, tagbuff);
					}
					context.put(code, tagbuff.toString());

					break;

				case 3: // 后台交易
					break;

				default:
					throw new Exception("templateParamType [" + ttype
							+ "] not found.");
				}
			}

		} catch (Exception e) {
			logger.error("constructionTemplet error", e);
			throw e;
		} finally {

		}
	}

	public void tagGenerator(String id, String pvalue, StringBuffer buff) {

		Validate.notNull(configuration, "HTMLTagTemplet.xml init error.");
		String tagTemplet = configuration.getString(id);
		Validate.notEmpty(tagTemplet, "tag [" + id + "] not found.");

		String[] arr = StringUtils.split(pvalue, ",");
		if (ArrayUtils.isEmpty(arr))
			return;

		for (String v : arr) {
			String[] kval = StringUtils.split(v, "=");
			if (ArrayUtils.isEmpty(kval))
				continue;
			tagTemplet = StringUtils.replace(tagTemplet, "{attr_" + kval[0]
					+ "}", kval[1]);
		}
		buff.append(tagTemplet);

	}

	static {
		try {
			configuration = new XMLConfiguration(
					"/Users/ZhangHao/Documents/workspace/macms/src/main/java/com/yuchengtech/core/code/generator/veloctity/html/HTMLTagTemplet.xml");
		} catch (ConfigurationException e) {
			configuration = null;
			logger.error("HTMLTagTemplet.xml init error.", e);
		}
	}

}
